package q.test0421library.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import q.test0421library.Mapper.BookMapper;
import q.test0421library.Mapper.BookMapperXml;
import q.test0421library.Moudle.BookInfo;
import q.test0421library.Moudle.BookStatus;
import q.test0421library.Moudle.PageRequest;
import q.test0421library.Moudle.PageResult;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper mapper;
    @Autowired
    private BookMapperXml mapperXml;

    public String addBook(BookInfo book) {

       Integer n= mapper.addBook(book);
       if(n!=null&&n!=0) return "";
       return "插入错误";
    }

    public PageResult<BookInfo> getListByPage(PageRequest request) {
        int count=mapper.count();
        List<BookInfo> list=mapper.getListByPage(request);

        for(BookInfo info:list){
            info.setStatusCN(BookStatus.getStatusBycode(info.getStatus()).getStatus());
        }

        PageResult<BookInfo> result=new PageResult<>(count,list,request);
        return result;


    }

    public BookInfo queryBookById(Integer bookId) {
        BookInfo bookInfo=mapper.queryBookById(bookId);
        return bookInfo;
    }


    public String updateBook(BookInfo bookInfo){

        Integer respone=mapperXml.updateBook(bookInfo);

        if(respone==0) return "添加失败";

        return "";
    }



    public boolean batchDeleteBook(List<Integer> ids) {
        return mapperXml.batchDeleteBook(ids);
    }
}
